package com.lanou.tipsy.adapter.rank;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.rank.Content;
import com.lanou.tipsy.entity.rank.ContentResult;

import java.util.ArrayList;

/**
 * Created by wangshuai on 15/10/27.
 *
 */
public class RankListAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<ContentResult> data;


    public RankListAdapter(Context mContext, ArrayList<ContentResult> data) {
        this.mContext = mContext;
        this.data = data;

    }
    @Override
    public int getCount() {
        if (data != null && data.size() > 0)
            return data.size();
        else
            return 0;
    }

    @Override
    public ContentResult getItem(int position) {
        if (data != null && data.size() > 0)
            return data.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_rank, null);
            //初始化
            holder = new ViewHolder();
            //绑定布局文件
            holder.my_image_view = (SimpleDraweeView) convertView.findViewById(R.id.my_image_view);
            holder.firstMusicName = (TextView) convertView.findViewById(R.id.firstMusicName);
            holder.firstMusicAuthor = (TextView) convertView.findViewById(R.id.firstMusicAuthor);
            holder.secondMusicName = (TextView) convertView.findViewById(R.id.secondMusicName);
            holder.secondMusicAuthor = (TextView) convertView.findViewById(R.id.secondMusicAuthor);
            holder.thirdMusicName = (TextView) convertView.findViewById(R.id.thirdMusicName);
            holder.thirdMusicAuthor = (TextView) convertView.findViewById(R.id.thirdMusicAuthor);
            holder.musicRank = (TextView) convertView.findViewById(R.id.musicRank);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ContentResult contentResult = getItem(position);
        if (contentResult != null) {
            //fraso方法加载图片,方法
            Uri uri = Uri.parse(contentResult.getPic_s192());
            holder.my_image_view.setImageURI(uri);
            holder.musicRank.setText(contentResult.getName());
        }
        Content content0 = contentResult.getContent().get(0);
        if (content0 != null) {
            holder.firstMusicName.setText(content0.getTitle());
            holder.firstMusicAuthor.setText(content0.getAuthor());
        }
        Content content1 = contentResult.getContent().get(1);
        if (content1 != null) {
            holder.secondMusicName.setText(content1.getTitle());
            holder.secondMusicAuthor.setText(content1.getAuthor());
        }
        Content content2 = contentResult.getContent().get(2);
        if (content2 != null) {
            holder.thirdMusicName.setText(content2.getTitle());
            holder.thirdMusicAuthor.setText(content2.getAuthor());
        }
        return convertView;
    }
    private class ViewHolder {
        private SimpleDraweeView my_image_view; // 榜单图片
        private TextView firstMusicName;  //排行第一的歌名
        private TextView firstMusicAuthor;//第一的作者
        private TextView secondMusicName;//排行第二的歌名
        private TextView secondMusicAuthor;//第二大作者
        private TextView thirdMusicName; //排行第三的歌名
        private TextView thirdMusicAuthor; //第三的作者
        private TextView musicRank;  //排行榜的名字
    }
}
